#include <iostream>
#include <algorithm>
#include <cstring>

using namespace std;

struct Edge{
    int a, b, w;
};

const int N = 1010, M = N * N;

int n, m, cnt;
int p[M];
int id[N][N];
Edge edge[2 * M];

int find(int x){
    if(x != p[x]){
        p[x] = find(p[x]);
    }

    return p[x];
}

void init(){
    for(int i = 1, k = 1; i <= n; i ++){
        for(int j = 1; j <= m; j ++, k ++){
            id[i][j] = k;
        }
    }

    for(int i = 1; i <= n * m; i ++){
        p[i] = i;
    }

   
    for(int i = 1; i + 1 <= n; i ++){
        for(int j = 1; j <= m; j ++){
            int x = id[i][j], y = id[i + 1][j];
            edge[cnt].a = x;
            edge[cnt].b = y;
            edge[cnt].w = 1;
            cnt ++;
        }
    }

     for(int i = 1; i <= n; i ++){
        for(int j = 1; j + 1 <= m; j ++){
            int x = id[i][j], y = id[i][j + 1];
            edge[cnt].a = x;
            edge[cnt].b = y;
            edge[cnt].w = 2;
            cnt ++;
        }
    }
}

int main(){
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);

    scanf("%d%d", &n, &m);

    init();
    int res = 0;
    int a1, b1, a2, b2;
    while(cin >> a1 >> b1 >> a2 >> b2){
        int a = id[a1][b1], b = id[a2][b2];
        int x = find(a), y = find(b);
        p[x] = p[y];
    }
    


    for(int i = 0; i < cnt; i ++){
        int x = edge[i].a;
        int y = edge[i].b;
        int a = find(x), b = find(y);
        if(a != b){
            p[a] = p[b];
            res += edge[i].w;
        }
    }



    cout << res << endl;

    return 0;
}